package com.picket.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.picket.dao.IPoolDao;
import com.picket.domain.Event;
import com.picket.domain.Match;
import com.picket.domain.Pool;
import com.picket.domain.UserPool;
import com.picket.domain.UserPrediction;

@Service
public class PoolService implements IPoolService {

	private IPoolDao poolDao;
	private IEventService eventService;
	private UserService userService;
	private IUserPoolService userPoolService;
	private IUserPredictionService userPredictionService;
	private IMatchService matchService;
	
	@Required
	public void setUserPoolService(IUserPoolService userPoolService) {
		this.userPoolService = userPoolService;
	}

	@Required
	public void setUserPredictionService(
			IUserPredictionService userPredictionService) {
		this.userPredictionService = userPredictionService;
	}


	@Required
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	

	@Required
	public void setEventService(
			IEventService eventService) {
		this.eventService = eventService;
	}
	
	public void setMatchService(IMatchService matchService) {
		this.matchService = matchService;
	}
	
	@Required
	public void setPoolDao(final IPoolDao eventDao) {
		this.poolDao = eventDao;
	}

	@Override
	public Pool create(final Pool pool) {
		pool.setCreateDate(Calendar.getInstance().getTime());
		pool.generateReferralKey();
		return poolDao.create(pool);
	}

	@Override
	public Collection<Pool> getAllPools() {
		return poolDao.getAllPools();
	}

	@Override
	public Pool getPool(long id) {
		return poolDao.getPool(id);
	}

	@Override
	public Pool findByReferral(String referralKey){
		return poolDao.findByReferral(referralKey);
	}
	
	 @Override
	 public Long createAllPoolRelatedInfo(final Pool pool) {
		 
		 // userPool info
		 final User user = userService.getCurrentUser();
		 
		 Event event = eventService.getEvent(pool.getEventId());
		 //String userId, String eventName, Long eventId,String poolKeyword, String poolOwnerUserEmail, String poolName,Long poolId

		 UserPool userPool = new UserPool(user.getUserId(),user.getEmail(), event.getName(),event.getId(),pool.getReferralKey(),pool.getOwner(),pool.getName(),pool.getId());
		 userPool=userPoolService.create(userPool); //should have key now
		 //userPrediction setup
		 
		 Collection<Match> matches = matchService.getAllMatchesForEvent(pool.getEventId());
		 List<UserPrediction> userPredictions = new ArrayList<UserPrediction>();
		 for(Match match: matches){
			 // Long userPoolId, Long matchId,Integer matchNo, String matchSummary,String userEmail, String updateUserId
			 //NYD - Not Yet Decided
			 UserPrediction userPrediction = new UserPrediction(userPool.getId(),match.getId(),
					 match.getMatchNo(),match.getSummary(),"NYD",match.getMatchDateTime(),user.getEmail(),user.getEmail());
			 userPredictions.add(userPrediction);
		 }
		userPredictionService.create(userPredictions);
		 // use the poolowner as user as only the owner can create a pool
	  //return poolDao.create(pool);
		return pool.getId();
		 
	 }


}
